package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2022/11/15 21:26
 */
@RestController
public class CookieSessionController {

    @RequestMapping("reqAndResp")
    public BaseRespVo reqAndResp(HttpServletRequest request, HttpServletResponse response) {
        return BaseRespVo.ok();
    }

    @RequestMapping("cookies")
    public BaseRespVo cookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("songge".equals(cookie.getName())) {
                System.out.println("*********" + cookie.getValue());
            }
            System.out.println(cookie.getName() + " --> " + cookie.getValue() );
        }
        return BaseRespVo.ok();
    }
    // localhost:8080/session1?username=songge
    @RequestMapping("session1")
    public BaseRespVo session1(HttpServletRequest request,String username) {
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        return BaseRespVo.ok();
    }
    @RequestMapping("session2")
    public BaseRespVo session2(HttpSession session) {
        Object username = session.getAttribute("username");
        return BaseRespVo.ok(username);
    }
}
